
<?php
import("@.Action.CommonAction");
class ClothingImportSheetAction extends Common {

	 public function Edit(){
		$Model = new Model();
		$ClothingImportSheetID = $_GET['ClothingImportSheetID'];
		$ClothingImportSheetList = "";
		$SizeNameList = null;
		$ColorNameList = null;

		if($ClothingImportSheetID != "")
		{
			$sqlB = new StringBuilder();
			$sqlB->Append("SELECT ");
			$sqlB->Append("   * ");
			$sqlB->Append("FROM  ClothingImportSheet ");
			$sqlB->Append("WHERE ClothingImportSheet.ID = " . $ClothingImportSheetID . " ");
			$sqlB->Append("LIMIT 1");
			$sqlB->Append(";");

			$ClothingImportSheetList = $Model->query($sqlB->ToString());
			$ClothingImportSheetList[0]['item_name'] = trim(str_replace($ClothingImportSheetList[0]['brand_name_en'], "", $ClothingImportSheetList[0]['item_name']));
			$ClothingImportSheetList[0]['item_name'] = trim(str_replace($ClothingImportSheetList[0]['brand_name_zh'], "", $ClothingImportSheetList[0]['item_name']));


			$sqlB = new StringBuilder();
			$sqlB->Append("SELECT ");
			$sqlB->Append("   ProductSubTypes.ProductSubType ");
			$sqlB->Append(" , ProductSubTypes.Code AS Code1 ");
			$sqlB->Append(" , SecondProductSubTypes.SecondProductSubType ");
			$sqlB->Append(" , SecondProductSubTypes.Code AS Code2 ");
			$sqlB->Append(" , ThirdProductSubTypes.ThirdProductSubType ");
			$sqlB->Append(" , ThirdProductSubTypes.Code AS Code3  ");
			$sqlB->Append(" , FourthProductSubTypes.FourthProductSubType ");
			$sqlB->Append(" , FourthProductSubTypes.Code AS Code4 ");
			$sqlB->Append(" , FifthProductSubTypes.FifthProductSubType ");
			$sqlB->Append(" , FifthProductSubTypes.Code AS Code5  ");
			$sqlB->Append(" , SixthProductSubTypes.SixthProductSubType ");
			$sqlB->Append(" , SixthProductSubTypes.Code AS Code6  ");
			$sqlB->Append(" , SeventhProductSubTypes.SeventhProductSubType ");
			$sqlB->Append(" , SeventhProductSubTypes.Code AS Code7  ");
			$sqlB->Append("FROM ProductSubTypes ");
			$sqlB->Append("LEFT JOIN  SecondProductSubTypes ON ProductSubTypes.Code = SecondProductSubTypes.ParentCode ");
			$sqlB->Append("LEFT JOIN  ThirdProductSubTypes ON SecondProductSubTypes.Code = ThirdProductSubTypes.ParentCode ");
			$sqlB->Append("LEFT JOIN  FourthProductSubTypes ON ThirdProductSubTypes.Code = FourthProductSubTypes.ParentCode ");
			$sqlB->Append("LEFT JOIN  FifthProductSubTypes ON FourthProductSubTypes.Code = FifthProductSubTypes.ParentCode ");
			$sqlB->Append("LEFT JOIN  SixthProductSubTypes ON FifthProductSubTypes.Code = SixthProductSubTypes.ParentCode ");
			$sqlB->Append("LEFT JOIN  SeventhProductSubTypes ON SixthProductSubTypes.Code = SeventhProductSubTypes.ParentCode ");
			$sqlB->Append("WHERE ");
			$sqlB->Append("ProductSubTypes.Code = " . $ClothingImportSheetList[0]['recommended_browse_nodes1'] . " ");
			$sqlB->Append("OR SecondProductSubTypes.Code = " . $ClothingImportSheetList[0]['recommended_browse_nodes1'] . " ");
			$sqlB->Append("OR ThirdProductSubTypes.Code = " . $ClothingImportSheetList[0]['recommended_browse_nodes1'] . " ");
			$sqlB->Append("OR FourthProductSubTypes.Code = " . $ClothingImportSheetList[0]['recommended_browse_nodes1'] . " ");
			$sqlB->Append("OR FifthProductSubTypes.Code = " . $ClothingImportSheetList[0]['recommended_browse_nodes1'] . " ");
			$sqlB->Append("OR SixthProductSubTypes.Code = " . $ClothingImportSheetList[0]['recommended_browse_nodes1'] . " ");
			$sqlB->Append("OR SeventhProductSubTypes.Code = " . $ClothingImportSheetList[0]['recommended_browse_nodes1'] . " ");

			$sqlB->Append("LIMIT 1 ");
			$sqlB->Append(";");

			$ProductSubTypesList = $Model->query($sqlB->ToString());

			$sqlB = new StringBuilder();
			$sqlB->Append("SELECT ");
			$sqlB->Append("   ID ");
			$sqlB->Append(" , ParentID ");
			$sqlB->Append(" , size_name ");
			$sqlB->Append("FROM  ClothingImportTransSheet ");
			$sqlB->Append("WHERE ClothingImportTransSheet.ParentID = " . $ClothingImportSheetID . " ");
			$sqlB->Append("GROUP BY size_name ");
			$sqlB->Append(";");
			$SizeNameList = $Model->query($sqlB->ToString());

			$sqlB = new StringBuilder();
			$sqlB->Append("SELECT ");
			$sqlB->Append("   ID ");
			$sqlB->Append(" , ParentID ");
			$sqlB->Append(" , color_name ");
			//$sqlB->Append(" , color_map ");
			$sqlB->Append("FROM  ClothingImportTransSheet ");
			$sqlB->Append("WHERE ClothingImportTransSheet.ParentID = " . $ClothingImportSheetID . " ");
			$sqlB->Append("GROUP BY color_name ");
			$sqlB->Append(";");
			$ColorNameList = $Model->query($sqlB->ToString());
			
		}

		$ProductSubTypes = $this->GetSelectVale($Model, "ProductSubTypes", "ProductSubtype = '服装'");
		$this->assign('ProductSubTypes',$ProductSubTypes);
		$this->assign('ClothingImportSheetList',$ClothingImportSheetList);
		$this->assign('SizeNameList',$SizeNameList);
		$this->assign('ColorNameList',$ColorNameList);
		$this->assign('ProductSubTypesList',$ProductSubTypesList);

		if($ClothingImportSheetList != ""){
			$this->display("EditClothingImportSheet");
		}
		else{
			$this->display("CreateClothingImportSheet");
		}
	 }

	 public function Deleted(){
		$ImportSheetID = $_GET['ImportSheetID'];
		$ClothingImportSheetID =  $_GET['ClothingImportSheetID'];

		$Model = new Model();

		$sqlB = new StringBuilder();
		$sqlB->Append("DELETE FROM ");
		$sqlB->Append("   ClothingImportSheet ");
		$sqlB->Append("WHERE ID = " . $ClothingImportSheetID);
		$sqlB->Append(";");
		$result = $Model->execute($sqlB->ToString());

		
		$sqlB = new StringBuilder();
		$sqlB->Append("DELETE FROM ");
		$sqlB->Append("   ClothingImportTransSheet ");
		$sqlB->Append("WHERE ClothingImportTransSheet.ParentID = " . $ClothingImportSheetID);
		$sqlB->Append(";");

		$result = $Model->execute($sqlB->ToString());

		$this->redirect("/Index");
	 }

	Public function UploadImage(){

		import("ORG.UploadFile");
		$ClothingImportSheetID = $_POST['ClothingImportSheetID'];
		$ColorName = $_POST['ColorName'];
		$ImageType = $_POST['ImageType'];

		$Model = new Model();
		$jsonencode = "";
		$upload = new UploadFile();// 实例化上传类
		$upload->maxSize  = 3145728 ;// 设置附件上传大小
		$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->savePath =  '../Uploads/'.$_SESSION['UserID']. "/";// 设置附件上传目录
		$upload->uploadReplace = true;
		 $upload->thumb = true;
        // 设置引用图片类库包路径
            $upload->imageClassPath ='ORG.Util.Image';
        //设置需要生成缩略图的文件后缀
            $upload->thumbPrefix='s_';  //生产2张缩略图
        //设置缩略图最大宽度
            $upload->thumbMaxWidth='100';
        //设置缩略图最大高度
            $upload->thumbMaxHeight='100';
        //设置上传文件规则
            $upload->saveRule='uniqid';
        //删除原图
            $upload->thumbRemoveOrigin=false;
		if(!$upload->upload()) {// 上传错误提示错误信息
			$this->error($upload->getErrorMsg());
		}else{// 上传成功 获取上传文件信息
				//更新ImportProductSheet中的主图和附图
			$info =  $upload->getUploadFileInfo();

			if($ImageType == "main"){
				$sqlB = new StringBuilder();
				$sqlB->Append("UPDATE ClothingImportSheet ");
				$sqlB->Append("SET ");
				$sqlB->Append("   main_image_url =" . FData('/Uploads/'.$_SESSION['UserID']. "/" . $info[0]['savename'], "Text"));
				$sqlB->Append(" , swatch_image_url =" . FData('/Uploads/'.$_SESSION['UserID']. "/" . $info[0]['savename'], "Text"). " ");
				$sqlB->Append("WHERE ClothingImportSheet.ID = ".$ClothingImportSheetID . " ");
				$sqlB->Append("AND ClothingImportSheet.main_image_url IS NULL ");
				$sqlB->Append("AND ClothingImportSheet.swatch_image_url IS NULL ");
				$sqlB->Append(";");
				$Model->execute($sqlB->ToString());

				$sqlB = new StringBuilder();
				$sqlB->Append("UPDATE ClothingImportTransSheet ");
				$sqlB->Append("SET ");
				$sqlB->Append("   main_image_url =" . FData('/Uploads/'.$_SESSION['UserID']. "/" . $info[0]['savename'], "Text"));
				$sqlB->Append(" , swatch_image_url =" . FData('/Uploads/'.$_SESSION['UserID']. "/" . $info[0]['savename'], "Text") . " ");
				$sqlB->Append("WHERE ClothingImportTransSheet.ParentID = ".$ClothingImportSheetID . " ");
				$sqlB->Append("AND ClothingImportTransSheet.color_name = ".FData($ColorName, "Text") . " ");
				$sqlB->Append("AND ClothingImportTransSheet.main_image_url IS NULL ");
				$sqlB->Append("AND ClothingImportTransSheet.swatch_image_url IS NULL ");
				$sqlB->Append(";");
				$Model->execute($sqlB->ToString());
				
				$arr = array
					(
						'URLID'=> 'main_image_url',
						'URL'=>'/Uploads/'.$_SESSION['UserID']. "/s_" . $info[0]['savename']
					);

				$jsonencode = json_encode($arr);
			}
			else{ 
				$sqlB = new StringBuilder();
				$sqlB->Append("SELECT ");
				$sqlB->Append("   ClothingImportTransSheet.other_image_url1 ");
				$sqlB->Append(" , ClothingImportTransSheet.other_image_url2 ");
				$sqlB->Append(" , ClothingImportTransSheet.other_image_url3 ");
				$sqlB->Append(" , ClothingImportTransSheet.other_image_url4 ");
				$sqlB->Append(" , ClothingImportTransSheet.other_image_url5 ");
				$sqlB->Append(" , ClothingImportTransSheet.other_image_url6 ");
				$sqlB->Append(" , ClothingImportTransSheet.other_image_url7 ");
				$sqlB->Append(" , ClothingImportTransSheet.other_image_url8 ");
				$sqlB->Append("FROM ClothingImportTransSheet ");
				$sqlB->Append("WHERE ClothingImportTransSheet.ParentID = ".$ClothingImportSheetID . " ");
				$sqlB->Append("AND ClothingImportTransSheet.color_name = ".FData($ColorName, "Text") . " ");
				$sqlB->Append("ORDER BY ClothingImportTransSheet.ID ");
				$sqlB->Append("LIMIT 1 ");
				$sqlB->Append(";");

				$ClothingImporttTransSheetList = $Model->query($sqlB->ToString());

				$tempTableName = "tmp_" . date("Ymd_His") . '_' . rand(100,999);
				$sqlB = new StringBuilder();
				$sqlB->Append("CREATE TABLE " . $tempTableName . " ( ");
				$sqlB->Append("   `ID` varchar(50) DEFAULT NULL, ");
				$sqlB->Append("   `Url` varchar(50) DEFAULT NULL ");
				$sqlB->Append(" );");
				$Model->execute($sqlB->ToString());

				$sqlB = new StringBuilder();
				$sqlB->Append("INSERT INTO " . $tempTableName . " values ");

				for($i=1; $i <= 7; $i++){
					$URLValue = "NULL";
					if($ClothingImporttTransSheetList [0]['other_image_url'.$i] != Null && $ClothingImporttTransSheetList [0]['other_image_url'.$i] != ""&& $ClothingImporttTransSheetList [0]['other_image_url'.$i] != "Null"){
						$URLValue = "'". $ClothingImporttTransSheetList [0]['other_image_url'.$i]. "'";
					}

					$sqlB->Append("('other_image_url" . $i . "', " . $URLValue . "), ");

				}
				//为了最后一次循环 sql 的结束符号为；
				if($ImportProductTransSheetList [0]['other_image_url8'] != Null && $ClothingImporttTransSheetList [0]['other_image_url8'] != "" && $ClothingImporttTransSheetList [0]['other_image_url8'] != 'Null'){
					$sqlB->Append("('other_image_url8', '" . $ClothingImporttTransSheetList [0]['other_image_url8'] . "'); ");
				}else{
					$sqlB->Append("('other_image_url8', NULL); ");
				}

				
				$Model->execute($sqlB->ToString());

				$sqlB = new StringBuilder();
				$sqlB->Append("SELECT `ID` ");
				$sqlB->Append("FROM " . $tempTableName . " AS tempTableName ");
				$sqlB->Append("WHERE tempTableName.Url IS NULL ");
				$sqlB->Append("ORDER BY tempTableName.`ID` ");
				$sqlB->Append("Limit 1 ");
				$sqlB->Append(";");
				$List = $Model->query($sqlB->ToString());
				$Model->execute("DROP TABLE IF EXISTS " . $tempTableName. "; ");
			
				if($List[0]['ID'] != ""){
					$sqlB = new StringBuilder();
					$sqlB->Append("UPDATE ClothingImportTransSheet ");
					$sqlB->Append("SET ");
					$sqlB->Append("   ClothingImportTransSheet." . $List[0]['ID'] . " = " . FData('/Uploads/'.$_SESSION['UserID']. "/" . $info[0]['savename'], "Text") . " ");
					$sqlB->Append("WHERE ClothingImportTransSheet.ParentID = ".$ClothingImportSheetID . " ");
					$sqlB->Append("AND ClothingImportTransSheet.color_name = ".FData($ColorName, "Text") . " ");
					$sqlB->Append(";");
					$Model->execute($sqlB->ToString());

					$sqlB = new StringBuilder();
					$sqlB->Append("UPDATE ClothingImportSheet ");
					$sqlB->Append("SET ");
					$sqlB->Append("   ClothingImportSheet." . $List[0]['ID'] . " = " . FData('/Uploads/'.$_SESSION['UserID']. "/" . $info[0]['savename'], "Text") . " ");
					$sqlB->Append("WHERE ClothingImportSheet.ID = ".$ClothingImportSheetID . " ");
					$sqlB->Append("AND ClothingImportSheet." . $List[0]['ID'] . " IS NULL ");
					$sqlB->Append(";");
					$Model->execute($sqlB->ToString());

					$arr = array
					(
						'URLID'=> $List[0]['ID'],
						'URL'=>'/Uploads/'.$_SESSION['UserID']. "/s_" . $info[0]['savename']
					);

					$jsonencode = json_encode($arr);
				}
			}
		}
		echo $jsonencode;
	}


	Public function GetSelectVale($Model, $TableName, $Where = ""){
	 
	 	$sqlB = new StringBuilder();
		$sqlB->Append("SELECT ");
		$sqlB->Append("   * ");
		$sqlB->Append("FROM  " . $TableName . " ");
		if($Where != ""){
			$sqlB->Append(" WHERE " . $Where . " ");
		}
		$sqlB->Append(";");
	
		$TableList = $Model->query($sqlB->ToString());
		return $TableList;
	 }


	Public function Add(){
		$Model = new Model();

		$ProductSize = null;
		$ProductColor = null;

		$ProductSize = array_filter($_POST['ProductSize']);
		$ProductColor = array_filter($_POST['ProductColor']);

		$ProductSubCount = Count($ProductSize) * Count($ProductColor);

		$ProductSubTypeID = $_POST['ProductSubTypeID'];
		$SecondProductSubTypeID = $_POST['SecondProductSubTypeID'];
		$ThirdProductSubTypeID = $_POST['ThirdProductSubTypeID'];
		$FourthProductSubTypeID = $_POST['FourthProductSubTypeID'];
		$FifthProductSubTypeID = $_POST['FifthProductSubTypeID'];
		$SixthProductSubTypeID = $_POST['SixthProductSubTypeID'];
		$SeventhProductSubTypeID = $_POST['SeventhProductSubTypeID'];

		$product_subtype =  $_POST['product_subtype'];

		$ProductSubTypeArr = array($ProductSubTypeID, $SecondProductSubTypeID, $ThirdProductSubTypeID, $FourthProductSubTypeID, $FifthProductSubTypeID, $SixthProductSubTypeID, $SeventhProductSubTypeID);

		$recommended_browse_nodes = array_pop(array_filter($ProductSubTypeArr));


		$brand_name_en = trim($_POST['brand_name_en']);
		$brand_name_zh = trim($_POST['brand_name_zh']);
		$temp_item_name = trim($_POST['item_name']);
		$manufacturer = trim($_POST['manufacturer']);
		//$country_of_origin = trim($_POST['country_of_origin']);
		//$sole_material = trim($_POST['sole_material']);
		$sole_material= "人造";

		$item_name = "";
		$brand_name = "";

		if($brand_name_en != ""){
			$item_name .= $brand_name_en .  " ";
			$brand_name .= $brand_name_en .  " ";
		}

		if($brand_name_zh != ""){
			$item_name .= $brand_name_zh .  " ";
			$brand_name .= $brand_name_zh .  " ";
		}

		if($temp_item_name != ""){
			$item_name .= $temp_item_name .  " ";
		}

		$sqlB = new StringBuilder();
		$sqlB->Append("SELECT ");
		$sqlB->Append("   ImportSheet.ImportSheetID ");
		$sqlB->Append("FROM  ImportSheet ");
		$sqlB->Append("WHERE DATE_FORMAT(ImportSheet.`Date`,'%Y-%m-%d')= '". date("Y-m-d", time()). "'");
		$sqlB->Append("AND ImportSheet.UserID = ".$_SESSION['UserID'] . " ");
		$sqlB->Append("Limit 1;");

		$ImportSheetList = $Model->query($sqlB->ToString());

		$sqlB = new StringBuilder();
		$sqlB->Append("INSERT INTO ");
		$sqlB->Append("   ClothingImportSheet ( ");
		$sqlB->Append("   ImportSheetID ");
		$sqlB->Append(" , item_name ");
		$sqlB->Append(" , brand_name ");
		$sqlB->Append(" , brand_name_en ");
		$sqlB->Append(" , brand_name_zh ");
		$sqlB->Append(" , product_subtype ");
		$sqlB->Append(" , manufacturer ");
		$sqlB->Append(" , recommended_browse_nodes1 ");
		//$sqlB->Append(" , country_of_origin ");
		$sqlB->Append(" , sole_material ");
		$sqlB->Append(" , currency ");
		$sqlB->Append(" , update_delete ");
		$sqlB->Append(" , parent_child ");
		$sqlB->Append(" , variation_theme ");
		$sqlB->Append(" , TimeUpdated ");
		$sqlB->Append(" , UpdatedBy ");
		$sqlB->Append(" , TimeCreated ");
		$sqlB->Append(" , CreatedBy ");
		$sqlB->Append("   ) ");
		$sqlB->Append("SELECT ");
		$sqlB->Append("   " . $ImportSheetList[0]['ImportSheetID'] . " AS ImportSheetID ");
		$sqlB->Append(" , " . FData($item_name, "Text") . " AS item_name ");
		$sqlB->Append(" , " . FData($brand_name, "Text") . " AS brand_name ");
		$sqlB->Append(" , " . FData($brand_name_en, "Text") . " AS brand_name_en ");
		$sqlB->Append(" , " . FData($brand_name_zh, "Text") . " AS brand_name_zh ");
		$sqlB->Append(" , " . FData($product_subtype, "Text") . " AS product_subtype ");
		$sqlB->Append(" , " . FData($manufacturer, "Text") . " AS manufacturer ");
		$sqlB->Append(" , " . FData($recommended_browse_nodes, "Text") . " AS recommended_browse_nodes1 ");
		//$sqlB->Append(" , " . FData($country_of_origin, "Text") . " AS country_of_origin ");
		$sqlB->Append(" , " . FData($sole_material, "Text") . " AS sole_material ");
		$sqlB->Append(" , '人民币' AS currency ");
		$sqlB->Append(" , '更新' AS update_delete ");
		$sqlB->Append(" , '父商品' AS parent_child ");

		if(Count($ProductSize)>0 && Count($ProductColor)>0){
			$sqlB->Append(" , '尺寸颜色' AS variation_theme ");
		}
		elseif(Count($ProductSize) == 0 && Count($ProductColor)>0){
			$sqlB->Append(" , '颜色' AS variation_theme ");
		}
		elseif(Count($ProductSize) > 0 && Count($ProductColor) == 0){
			$sqlB->Append(" , '尺寸' AS variation_theme ");
		}
		
		$sqlB->Append(" , NOW() AS TimeUpdated ");
		$sqlB->Append(" , '". $_SESSION['UserName'] . "' AS UpdatedBy ");
		$sqlB->Append(" , NOW() AS TimeCreated ");
		$sqlB->Append(" , '". $_SESSION['UserName'] . "' AS CreatedBy ");
		$sqlB->Append(";");

		$ClothingImportSheetIDStr = $Model->execute($sqlB->ToString());
		$ClothingImportSheetIDArr = explode("^", $ClothingImportSheetIDStr);
		
		if($ClothingImportSheetIDArr[0]){
		
			for ($i =0; $i< Count($ProductSize); $i++ )
			{
				for ($j =0; $j< Count($ProductColor); $j++ )
				{
					
					$item_name_sub = $item_name;
					if(trim($ProductColor[$j]) != ""){
						$item_name_sub .= trim($ProductColor[$j]) .  " ";
					}

					if(trim($ProductSize[$i]) != ""){
						$item_name_sub .= trim($ProductSize[$i]) .  " ";
					}
					
					$sqlB = new StringBuilder();
					$sqlB->Append("INSERT INTO ");
					$sqlB->Append("   ClothingImportTransSheet ( ");
					$sqlB->Append("   ParentID ");
					$sqlB->Append(" , item_name ");
					$sqlB->Append(" , brand_name ");
					$sqlB->Append(" , color_name ");
					//$sqlB->Append(" , color_map ");
					$sqlB->Append(" , size_name ");
					$sqlB->Append(" , product_subtype ");
					$sqlB->Append(" , manufacturer ");
					
					$sqlB->Append(" , recommended_browse_nodes1 ");
					
					//$sqlB->Append(" , country_of_origin ");
					$sqlB->Append(" , sole_material ");

					$sqlB->Append(" , currency ");
					$sqlB->Append(" , update_delete ");
					$sqlB->Append(" , parent_child ");
					$sqlB->Append(" , variation_theme ");
					$sqlB->Append(" , relationship_type ");
					$sqlB->Append(" , TimeUpdated ");
					$sqlB->Append(" , UpdatedBy ");
					$sqlB->Append(" , TimeCreated ");
					$sqlB->Append(" , CreatedBy ");
					$sqlB->Append("   ) ");
					$sqlB->Append("SELECT ");
					$sqlB->Append("   " . $ClothingImportSheetIDArr[1] . " AS ParentID ");
					$sqlB->Append(" , " . FData($item_name_sub, "Text") . " AS item_name ");
					$sqlB->Append(" , " . FData($brand_name, "Text") . " AS brand_name ");
					$sqlB->Append(" , " . FData(trim($ProductColor[$j]), "Text") . " AS color_name ");
					//$sqlB->Append(" , '" . trim($ColorMap[$j]) . "' AS color_map ");
					$sqlB->Append(" , " . FData(trim($ProductSize[$i]), "Text") . " AS size_name ");
					$sqlB->Append(" , " . FData($product_subtype, "Text") . " AS product_subtype ");
					$sqlB->Append(" , " . FData($manufacturer, "Text") . " AS manufacturer ");
					$sqlB->Append(" , " . FData($recommended_browse_nodes, "Text") . " AS recommended_browse_nodes1 ");
					//$sqlB->Append(" , " . FData($country_of_origin, "Text") . " AS country_of_origin ");
					$sqlB->Append(" , " . FData($sole_material, "Text") . " AS sole_material ");

					$sqlB->Append(" , '人民币' AS currency ");
					$sqlB->Append(" , '更新' AS update_delete ");
					$sqlB->Append(" , '子商品' AS parent_child ");

					if(Count($ProductSize)>0 && Count($ProductColor)>0){
						$sqlB->Append(" , '尺寸颜色' AS variation_theme ");
					}
					elseif(Count($ProductSize) == 0 && Count($ProductColor)>0){
						$sqlB->Append(" , '颜色' AS variation_theme ");
					}
					elseif(Count($ProductSize) > 0 && Count($ProductColor) == 0){
						$sqlB->Append(" , '尺寸' AS variation_theme ");
					}
					
					$sqlB->Append(" , '变体' AS relationship_type ");

					$sqlB->Append(" , NOW() AS TimeUpdated ");
					$sqlB->Append(" , '". $_SESSION['UserName'] . "' AS UpdatedBy ");
					$sqlB->Append(" , NOW() AS TimeCreated ");
					$sqlB->Append(" , '". $_SESSION['UserName'] . "' AS CreatedBy ");
					$sqlB->Append(";");
					
					$Model->execute($sqlB->ToString());
				}
			}
		}


		if($ClothingImportSheetIDArr[1]){
			$this->redirect("/ClothingImportSheet/Edit2/ClothingImportSheetID/". $ClothingImportSheetIDArr[1] . "");
		}
		else{
			$this->error('添加失败！', "/Index");
		}
	}

	Public function ReEdit(){

		$ClothingImportSheetID = $_POST['ClothingImportSheetID'];
		if($ClothingImportSheetID == ""){
			return;
		}
		//Debug($_POST);
		$Model = new Model();

		$ProductSize = null;
		$ProductColor = null;
		//$ColorMap = null;

		$ProductSize = array_filter($_POST['ProductSize']);
		$ProductColor = array_filter($_POST['ProductColor']);
		//$ColorMap = array_filter($_POST['color_map']);
		
		$ProductSubCount = Count($ProductSize) * Count($ProductColor);
		$ProductSubTypeID = $_POST['ProductSubTypeID'];
		$SecondProductSubTypeID = $_POST['SecondProductSubTypeID'];
		$ThirdProductSubTypeID = $_POST['ThirdProductSubTypeID'];
		$FourthProductSubTypeID = $_POST['FourthProductSubTypeID'];
		$FifthProductSubTypeID = $_POST['FifthProductSubTypeID'];

		$ProductSubTypeArr = array($ProductSubTypeID, $SecondProductSubTypeID, $ThirdProductSubTypeID, $FourthProductSubTypeID, $FifthProductSubTypeID);
		$recommended_browse_nodes = array_pop(array_filter($ProductSubTypeArr));

		$brand_name_en = trim($_POST['brand_name_en']);
		$brand_name_zh = trim($_POST['brand_name_zh']);
		$temp_item_name = trim($_POST['item_name']);
		$manufacturer = trim($_POST['manufacturer']);
		$country_of_origin = trim($_POST['country_of_origin']);
		$sole_material= "人造";
		
		$product_subtype = trim($_POST['product_subtype']);
		$item_name = "";
		$brand_name = "";

		if($brand_name_en != ""){
			$item_name .= $brand_name_en .  " ";
			$brand_name .= $brand_name_en .  " ";
		}

		if($brand_name_zh != ""){
			$item_name .= $brand_name_zh .  " ";
			$brand_name .= $brand_name_zh .  " ";
		}

		if($temp_item_name != ""){
			$item_name .= $temp_item_name .  " ";
		}

		$sqlB = new StringBuilder();
		$sqlB->Append("UPDATE  ");
		$sqlB->Append("   ClothingImportSheet  ");
		$sqlB->Append(" SET ");
		$sqlB->Append("   item_name = " .  FData($item_name, "Text") . " ");
		$sqlB->Append(" , product_subtype = " .  FData($product_subtype, "Text") . " ");
		
		$sqlB->Append(" , brand_name = " . FData($brand_name, "Text") . " ");
		$sqlB->Append(" , brand_name_en = " . FData($brand_name_en, "Text") . " ");
		$sqlB->Append(" , brand_name_zh = " . FData($brand_name_zh, "Text") . " ");
		$sqlB->Append(" , manufacturer = " . FData($manufacturer, "Text") . " ");
		$sqlB->Append(" , recommended_browse_nodes1 = " . FData($recommended_browse_nodes, "Text") . " ");

		$sqlB->Append(" , sole_material = ". FData($sole_material, "Text"). " ");
		$sqlB->Append(" , variation_theme");
		
		if(Count($ProductSize)>0 && Count($ProductColor)>0){
			$sqlB->Append(" = '尺寸颜色' ");
		}
		elseif(Count($ProductSize) == 0 && Count($ProductColor)>0){
			$sqlB->Append(" = '颜色' ");
		}
		elseif(Count($ProductSize) > 0 && Count($ProductColor) == 0){
			$sqlB->Append(" = '尺寸' ");
		}
		$sqlB->Append(" , TimeUpdated =  NOW() ");
		$sqlB->Append(" , UpdatedBy = '". $_SESSION['UserName'] . "' ");
		$sqlB->Append("WHERE ID = " . $ClothingImportSheetID);
		$sqlB->Append(";");
		
		$ClothingImportSheetIDStr = $Model->execute($sqlB->ToString());
		$ClothingImportSheetIDArr = explode("^", $ClothingImportSheetIDStr);

		if($ClothingImportSheetIDArr[0]){
		
			for ($i =0; $i< Count($ProductSize); $i++ )
			{
				for ($j =0; $j< Count($ProductColor); $j++ )
				{
					
					$item_name_sub = $item_name;
					if(trim($ProductColor[$j]) != ""){
						$item_name_sub .= trim($ProductColor[$j]) .  " ";
					}

					if(trim($ProductSize[$i]) != ""){
						$item_name_sub .= trim($ProductSize[$i]) .  " ";
					}
							
					$sqlB = new StringBuilder();
					$sqlB->Append("SELECT ");
					$sqlB->Append("   ClothingImportTransSheet.ID ");
					$sqlB->Append("FROM  ClothingImportTransSheet ");
					$sqlB->Append("WHERE ParentID = " . $ClothingImportSheetID. " ");
					$sqlB->Append("AND color_name = '" . trim($ProductColor[$j]) . "' ");
					$sqlB->Append("AND size_name = '" . trim($ProductSize[$i])  . "' ");
					$sqlB->Append("Limit 1;");

					$ClothingImportTransSheetList = $Model->query($sqlB->ToString());

					if(Count($ClothingImportTransSheetList) == 1 ){
						$sqlB = new StringBuilder();

						$sqlB->Append("UPDATE  ");
						$sqlB->Append("   ClothingImportTransSheet  ");
						$sqlB->Append(" SET ");
						$sqlB->Append("   item_name = " . FData($item_name_sub, "Text") . " ");
						$sqlB->Append(" , brand_name = " . FData($brand_name, "Text") . " ");
						$sqlB->Append(" , color_name = " . FData(trim($ProductColor[$j]), "Text") . " ");
						$sqlB->Append(" , size_name = " . FData(trim($ProductSize[$i]), "Text") . " ");
						$sqlB->Append(" , manufacturer = " . FData($manufacturer, "Text") . " ");
						$sqlB->Append(" , recommended_browse_nodes1 = '" . FData($recommended_browse_nodes, "Text") . " ");
						$sqlB->Append(" , sole_material = " . FData($sole_material, "Text") . " ");
						$sqlB->Append(" , product_subtype = " .  FData($product_subtype, "Text") . " ");

						$sqlB->Append(" , variation_theme ");
						
						if(Count($ProductSize)>0 && Count($ProductColor)>0){
							$sqlB->Append(" = '尺寸颜色' ");
						}
						elseif(Count($ProductSize) == 0 && Count($ProductColor)>0){
							$sqlB->Append(" = '颜色' ");
						}
						elseif(Count($ProductSize) > 0 && Count($ProductColor) == 0){
							$sqlB->Append(" = '尺寸' ");
						}
						$sqlB->Append(" , TimeUpdated =  NOW() ");
						$sqlB->Append(" , UpdatedBy = '". $_SESSION['UserName'] . "' ");
						$sqlB->Append("WHERE ParentID = " . $ClothingImportSheetID. " ");
						$sqlB->Append("AND color_name = '" .trim($ProductColor[$j]) . "' ");
						$sqlB->Append("AND size_name = '" . trim($ProductSize[$i])  . "' ");
						$sqlB->Append(";");
						$Model->execute($sqlB->ToString());	
					}
					else{
					
						$sqlB = new StringBuilder();

						$sqlB->Append("INSERT INTO ");
						$sqlB->Append("   ClothingImportTransSheet ( ");
						$sqlB->Append("   ParentID ");
						$sqlB->Append(" , item_name ");
						$sqlB->Append(" , brand_name ");
						$sqlB->Append(" , color_name ");
						$sqlB->Append(" , size_name ");
						$sqlB->Append(" , product_subtype ");
						$sqlB->Append(" , manufacturer ");
						$sqlB->Append(" , recommended_browse_nodes1 ");
						$sqlB->Append(" , sole_material ");

						$sqlB->Append(" , currency ");
						$sqlB->Append(" , update_delete ");
						$sqlB->Append(" , parent_child ");
						$sqlB->Append(" , variation_theme ");
						$sqlB->Append(" , relationship_type ");
						$sqlB->Append(" , TimeUpdated ");
						$sqlB->Append(" , UpdatedBy ");
						$sqlB->Append(" , TimeCreated ");
						$sqlB->Append(" , CreatedBy ");
						$sqlB->Append("   ) ");
						$sqlB->Append("SELECT ");
						$sqlB->Append("   " . $ClothingImportSheetID . " AS ParentID ");
						$sqlB->Append(" , " . FData($item_name_sub, "Text") . " AS item_name ");
						$sqlB->Append(" , " . FData($brand_name, "Text") . " AS brand_name ");
						$sqlB->Append(" , " . FData(trim($ProductColor[$j]), "Text") . " AS color_name ");
						$sqlB->Append(" , " . FData(trim($ProductSize[$i]), "Text") . " AS size_name ");
						$sqlB->Append(" , " . FData($product_subtype, "Text") . " AS product_subtype ");
						$sqlB->Append(" , " . FData($manufacturer, "Text") . " AS manufacturer ");
						
						$sqlB->Append(" , " . FData($recommended_browse_nodes, "Text") . " AS recommended_browse_nodes1 ");
						//$sqlB->Append(" , " . FData($country_of_origin, "Text") . " AS country_of_origin ");
						$sqlB->Append(" , " . FData($sole_material, "Text") . " AS sole_material ");

						$sqlB->Append(" , '人民币' AS currency ");
						$sqlB->Append(" , '更新' AS update_delete ");
						$sqlB->Append(" , '子商品' AS parent_child ");

						if(Count($ProductSize)>0 && Count($ProductColor)>0){
							$sqlB->Append(" , '尺寸颜色' AS variation_theme ");
						}
						elseif(Count($ProductSize) == 0 && Count($ProductColor)>0){
							$sqlB->Append(" , '颜色' AS variation_theme ");
						}
						elseif(Count($ProductSize) > 0 && Count($ProductColor) == 0){
							$sqlB->Append(" , '尺寸' AS variation_theme ");
						}
						
						$sqlB->Append(" , '变体' AS relationship_type ");

						$sqlB->Append(" , NOW() AS TimeUpdated ");
						$sqlB->Append(" , '". $_SESSION['UserName'] . "' AS UpdatedBy ");
						$sqlB->Append(" , NOW() AS TimeCreated ");
						$sqlB->Append(" , '". $_SESSION['UserName'] . "' AS CreatedBy ");
						$sqlB->Append(";");

						$Model->execute($sqlB->ToString());
					}
				}
			}
		}

		if($ClothingImportSheetID){
			$this->redirect("/ClothingImportSheet/Edit2/ClothingImportSheetID/". $ClothingImportSheetID . "");
		}
		else{
			$this->error('编辑失败！', "/ClothingImportSheet/Edit2/ClothingImportSheetID/". $ClothingImportSheetID . "");
		}
	}

	function Edit2(){
	
		$Model = new Model();
		$ClothingImportSheetID = $_GET['ClothingImportSheetID'];

		$sqlB = new StringBuilder();
		$sqlB->Append("SELECT ");
		$sqlB->Append("   * ");
		$sqlB->Append("FROM  ClothingImportSheet ");
		$sqlB->Append("WHERE ClothingImportSheet.ID = " . $ClothingImportSheetID . " ");
		$sqlB->Append("LIMIT 1");
		$sqlB->Append(";");
		$ClothingImportSheetList = $Model->query($sqlB->ToString());

		$ClothingImportSheetList[0]['item_name'] = trim(str_replace($ClothingImportSheetList[0]['brand_name_en'], "", $ClothingImportSheetList[0]['item_name']));
		$ClothingImportSheetList[0]['item_name'] = trim(str_replace($ClothingImportSheetList[0]['brand_name_zh'], "", $ClothingImportSheetList[0]['item_name']));


		$sqlB = new StringBuilder();
		$sqlB->Append("SELECT ");
		$sqlB->Append("   ProductSubTypes.ProductSubType ");
		$sqlB->Append(" , SecondProductSubTypes.SecondProductSubType ");
		$sqlB->Append(" , ThirdProductSubTypes.ThirdProductSubType ");
		$sqlB->Append(" , FourthProductSubTypes.FourthProductSubType ");
		$sqlB->Append(" , FifthProductSubTypes.FifthProductSubType ");
		$sqlB->Append(" , SixthProductSubTypes.SixthProductSubType ");
		$sqlB->Append(" , SeventhProductSubTypes.SeventhProductSubType ");

		$sqlB->Append("FROM ProductSubTypes ");
		$sqlB->Append("LEFT JOIN  SecondProductSubTypes ON ProductSubTypes.Code = SecondProductSubTypes.ParentCode ");
		$sqlB->Append("LEFT JOIN  ThirdProductSubTypes ON SecondProductSubTypes.Code = ThirdProductSubTypes.ParentCode ");
		$sqlB->Append("LEFT JOIN  FourthProductSubTypes ON ThirdProductSubTypes.Code = FourthProductSubTypes.ParentCode ");
		$sqlB->Append("LEFT JOIN  FifthProductSubTypes ON FourthProductSubTypes.Code = FifthProductSubTypes.ParentCode ");

		$sqlB->Append("LEFT JOIN  SixthProductSubTypes ON FifthProductSubTypes.Code = SixthProductSubTypes.ParentCode ");
		$sqlB->Append("LEFT JOIN  SeventhProductSubTypes ON SixthProductSubTypes.Code = SeventhProductSubTypes.ParentCode ");

		$sqlB->Append("WHERE ");
		$sqlB->Append("ProductSubTypes.Code = " . $ClothingImportSheetList[0]['recommended_browse_nodes1'] . " ");
		$sqlB->Append("OR SecondProductSubTypes.Code = " . $ClothingImportSheetList[0]['recommended_browse_nodes1'] . " ");
		$sqlB->Append("OR ThirdProductSubTypes.Code = " . $ClothingImportSheetList[0]['recommended_browse_nodes1'] . " ");
		$sqlB->Append("OR FourthProductSubTypes.Code = " . $ClothingImportSheetList[0]['recommended_browse_nodes1'] . " ");
		$sqlB->Append("OR FifthProductSubTypes.Code = " . $ClothingImportSheetList[0]['recommended_browse_nodes1'] . " ");

		$sqlB->Append("OR SixthProductSubTypes.Code = " . $ClothingImportSheetList[0]['recommended_browse_nodes1'] . " ");
		$sqlB->Append("OR SeventhProductSubTypes.Code = " . $ClothingImportSheetList[0]['recommended_browse_nodes1'] . " ");
		$sqlB->Append("LIMIT 1 ");
		$sqlB->Append(";");

		$ProductSubTypesList = $Model->query($sqlB->ToString());

		$sqlB = new StringBuilder();
		$sqlB->Append("SELECT ");
		$sqlB->Append("   ClothingImportTransSheet.size_name ");
		$sqlB->Append("FROM ClothingImportTransSheet ");
		$sqlB->Append("WHERE ClothingImportTransSheet.ParentID = ".$ClothingImportSheetID . " ");
		$sqlB->Append("GROUP BY ClothingImportTransSheet.size_name ");
		$sqlB->Append("ORDER BY ClothingImportTransSheet.ID ;");
		$ProductSizeList = $Model->query($sqlB->ToString());


		$sqlB = new StringBuilder();
		$sqlB->Append("SELECT ");
		$sqlB->Append("   ClothingImportTransSheet.color_name ");
		//$sqlB->Append(" , ImportProductTransSheet.color_map ");
		$sqlB->Append(" , ClothingImportTransSheet.ID ");
		$sqlB->Append(" , ClothingImportTransSheet.main_image_url ");
		$sqlB->Append(" , ClothingImportTransSheet.other_image_url1 ");
		$sqlB->Append(" , ClothingImportTransSheet.other_image_url2 ");
		$sqlB->Append(" , ClothingImportTransSheet.other_image_url3 ");
		$sqlB->Append(" , ClothingImportTransSheet.other_image_url4 ");
		$sqlB->Append(" , ClothingImportTransSheet.other_image_url5 ");
		$sqlB->Append(" , ClothingImportTransSheet.other_image_url6 ");
		$sqlB->Append(" , ClothingImportTransSheet.other_image_url7 ");
		$sqlB->Append(" , ClothingImportTransSheet.other_image_url8 ");
		$sqlB->Append("FROM ClothingImportTransSheet ");
		$sqlB->Append("WHERE ClothingImportTransSheet.ParentID = ".$ClothingImportSheetID . " ");
		$sqlB->Append("GROUP BY ClothingImportTransSheet.color_name ");
		$sqlB->Append("ORDER BY ClothingImportTransSheet.ID ;");
		$ProductColorList = $Model->query($sqlB->ToString());

		$sqlB = new StringBuilder();
		$sqlB->Append("SELECT ");
		$sqlB->Append("   ClothingImportTransSheet.ID ");
		$sqlB->Append(" , ClothingImportTransSheet.color_name ");
		$sqlB->Append(" , ClothingImportTransSheet.size_name ");
		$sqlB->Append(" , ClothingImportTransSheet.item_sku ");
		$sqlB->Append(" , ClothingImportTransSheet.standard_price ");
		$sqlB->Append(" , ClothingImportTransSheet.sale_price ");
		$sqlB->Append(" , ClothingImportTransSheet.sale_from_date ");
		$sqlB->Append(" , ClothingImportTransSheet.sale_end_date ");
		$sqlB->Append(" , ClothingImportTransSheet.quantity ");
		$sqlB->Append(" , ClothingImportTransSheet.fulfillment_latency ");
		$sqlB->Append("FROM ClothingImportTransSheet ");
		$sqlB->Append("WHERE ClothingImportTransSheet.ParentID = ".$ClothingImportSheetID . " ");
		$sqlB->Append("ORDER BY ClothingImportTransSheet.ID ;");
		$ClothingImportTransSheetList = $Model->query($sqlB->ToString());
		
		$ProductSubTypes = $this->GetSelectVale($Model, "ProductSubTypes");

		
		$this->assign('ProductSubTypesList',$ProductSubTypesList);
		$this->assign('ClothingImportSheetList',$ClothingImportSheetList);
		$this->assign('ProductSizeList',$ProductSizeList);
		$this->assign('ProductColorList',$ProductColorList);
		$this->assign('ClothingImportTransSheetList',$ClothingImportTransSheetList);
		$this->assign('ProductSubTypes',$ProductSubTypes);
		$this->display("UpdateClothingImportSheet");
	}

	function View(){
	
		$Model = new Model();
		$ClothingImportSheetID = $_GET['ClothingImportSheetID'];

		$sqlB = new StringBuilder();
		$sqlB->Append("SELECT ");
		$sqlB->Append("   * ");
		$sqlB->Append("FROM  ClothingImportSheet ");
		$sqlB->Append("WHERE ClothingImportSheet.ID = " . $ClothingImportSheetID . " ");
		$sqlB->Append("LIMIT 1");
		$sqlB->Append(";");
		$ClothingImportSheetList = $Model->query($sqlB->ToString());
		
		$ClothingImportSheetList[0]['item_name'] = trim(str_replace($ClothingImportSheetList[0]['brand_name_en'], "", $ClothingImportSheetList[0]['item_name']));
		$ClothingImportSheetList[0]['item_name'] = trim(str_replace($ClothingImportSheetList[0]['brand_name_zh'], "", $ClothingImportSheetList[0]['item_name']));


		$sqlB = new StringBuilder();
		$sqlB->Append("SELECT ");
		$sqlB->Append("   ProductSubTypes.ProductSubType ");
		$sqlB->Append(" , SecondProductSubTypes.SecondProductSubType ");
		$sqlB->Append(" , ThirdProductSubTypes.ThirdProductSubType ");
		$sqlB->Append(" , FourthProductSubTypes.FourthProductSubType ");
		$sqlB->Append(" , FifthProductSubTypes.FifthProductSubType ");
		$sqlB->Append("FROM ProductSubTypes ");
		$sqlB->Append("LEFT JOIN  SecondProductSubTypes ON ProductSubTypes.Code = SecondProductSubTypes.ParentCode ");
		$sqlB->Append("LEFT JOIN  ThirdProductSubTypes ON SecondProductSubTypes.Code = ThirdProductSubTypes.ParentCode ");
		$sqlB->Append("LEFT JOIN  FourthProductSubTypes ON ThirdProductSubTypes.Code = FourthProductSubTypes.ParentCode ");
		$sqlB->Append("LEFT JOIN  FifthProductSubTypes ON FourthProductSubTypes.Code = FifthProductSubTypes.ParentCode ");

		$sqlB->Append("LEFT JOIN  SixthProductSubTypes ON FifthProductSubTypes.Code = SixthProductSubTypes.ParentCode ");
		$sqlB->Append("LEFT JOIN  SeventhProductSubTypes ON SixthProductSubTypes.Code = SeventhProductSubTypes.ParentCode ");

		$sqlB->Append("WHERE ");
		$sqlB->Append("ProductSubTypes.Code = " . $ClothingImportSheetList[0]['recommended_browse_nodes1'] . " ");
		$sqlB->Append("OR SecondProductSubTypes.Code = " . $ClothingImportSheetList[0]['recommended_browse_nodes1'] . " ");
		$sqlB->Append("OR ThirdProductSubTypes.Code = " . $ClothingImportSheetList[0]['recommended_browse_nodes1'] . " ");
		$sqlB->Append("OR FourthProductSubTypes.Code = " . $ClothingImportSheetList[0]['recommended_browse_nodes1'] . " ");
		$sqlB->Append("OR FifthProductSubTypes.Code = " . $ClothingImportSheetList[0]['recommended_browse_nodes1'] . " ");

		$sqlB->Append("OR SixthProductSubTypes.Code = " . $ClothingImportSheetList[0]['recommended_browse_nodes1'] . " ");
		$sqlB->Append("OR SeventhProductSubTypes.Code = " . $ClothingImportSheetList[0]['recommended_browse_nodes1'] . " ");
		$sqlB->Append("LIMIT 1 ");
		$sqlB->Append(";");

		$ProductSubTypesList = $Model->query($sqlB->ToString());

		$sqlB = new StringBuilder();
		$sqlB->Append("SELECT ");
		$sqlB->Append("   ClothingImportTransSheet.size_name ");
		$sqlB->Append("FROM ClothingImportTransSheet ");
		$sqlB->Append("WHERE ClothingImportTransSheet.ParentID = ".$ClothingImportSheetID . " ");
		$sqlB->Append("GROUP BY ClothingImportTransSheet.size_name ");
		$sqlB->Append("ORDER BY ClothingImportTransSheet.ID ;");
		$ProductSizeList = $Model->query($sqlB->ToString());


		$sqlB = new StringBuilder();
		$sqlB->Append("SELECT ");
		$sqlB->Append("   ClothingImportTransSheet.color_name ");
		//$sqlB->Append(" , ClothingImportTransSheet.color_map ");
		$sqlB->Append(" , ClothingImportTransSheet.main_image_url ");
		$sqlB->Append(" , ClothingImportTransSheet.other_image_url1 ");
		$sqlB->Append(" , ClothingImportTransSheet.other_image_url2 ");
		$sqlB->Append(" , ClothingImportTransSheet.other_image_url3 ");
		$sqlB->Append(" , ClothingImportTransSheet.other_image_url4 ");
		$sqlB->Append(" , ClothingImportTransSheet.other_image_url5 ");
		$sqlB->Append(" , ClothingImportTransSheet.other_image_url6 ");
		$sqlB->Append(" , ClothingImportTransSheet.other_image_url7 ");
		$sqlB->Append(" , ClothingImportTransSheet.other_image_url8 ");
		$sqlB->Append("FROM ClothingImportTransSheet ");
		$sqlB->Append("WHERE ClothingImportTransSheet.ParentID = ".$ClothingImportSheetID . " ");
		$sqlB->Append("GROUP BY ClothingImportTransSheet.color_name ");
		$sqlB->Append("ORDER BY ClothingImportTransSheet.ID ;");
		$ProductColorList = $Model->query($sqlB->ToString());

		$sqlB = new StringBuilder();
		$sqlB->Append("SELECT ");
		$sqlB->Append("   ClothingImportTransSheet.ID ");
		$sqlB->Append(" , ClothingImportTransSheet.color_name ");
		$sqlB->Append(" , ClothingImportTransSheet.size_name ");
		$sqlB->Append(" , ClothingImportTransSheet.item_sku ");
		$sqlB->Append(" , ClothingImportTransSheet.standard_price ");
		$sqlB->Append(" , ClothingImportTransSheet.sale_price ");
		$sqlB->Append(" , ClothingImportTransSheet.sale_from_date ");
		$sqlB->Append(" , ClothingImportTransSheet.sale_end_date ");
		$sqlB->Append(" , ClothingImportTransSheet.quantity ");
		$sqlB->Append(" , ClothingImportTransSheet.fulfillment_latency ");
		
		$sqlB->Append("FROM ClothingImportTransSheet ");
		$sqlB->Append("WHERE ClothingImportTransSheet.ParentID = ".$ClothingImportSheetID . " ");
		$sqlB->Append("ORDER BY ClothingImportTransSheet.ID ;");
		$ClothingImportTransSheetList = $Model->query($sqlB->ToString());
		
		//$ProductSubTypes = $this->GetSelectVale($Model, "ProductSubTypes");

		
		$this->assign('ProductSubTypesList',$ProductSubTypesList);
		$this->assign('ClothingImportSheetList',$ClothingImportSheetList);
		$this->assign('ProductSizeList',$ProductSizeList);
		$this->assign('ProductColorList',$ProductColorList);
		$this->assign('ClothingImportTransSheetList',$ClothingImportTransSheetList);
		
		//$this->assign('ProductSubTypes',$ProductSubTypes);
		$this->display("ViewClothingImportSheet");
	}

	function Update(){
		$Model = new Model();
		$ReturnUrl = $_GET['ReturnAction'];
		$ClothingImportSheetID = $_POST['ClothingImportSheetID'];
		//更新ImportProductSheet表 ：父商品表
		$product_description = $_POST['product_description'];
		$product_description = str_replace(Chr(13),"", $product_description);
		$product_description = str_replace(Chr(10),"<br />", $product_description);
		
		$safety_warning = $_POST['safety_warning'];
		$safety_warning = str_replace(Chr(13),"", $safety_warning);
		$safety_warning = str_replace(Chr(10),"<br />", $safety_warning);

		$sqlB = new StringBuilder();
		$sqlB->Append("UPDATE ClothingImportSheet ");
		$sqlB->Append("SET ");
		$sqlB->Append("   item_sku =" . FData($_POST['item_sku'][0], "Text"));

		if($product_description != ""){
			
			$sqlB->Append("  , product_description = '" . $product_description  . "' ");
		}
		else{
			$sqlB->Append("  , product_description = Null ");
		}
		
		if($safety_warning != ""){
			
			$sqlB->Append(" , safety_warning = '" . $safety_warning  . "' ");
		}
		else{
			$sqlB->Append("  , safety_warning = Null ");
		}

		$sqlB->Append(" , country_of_origin =  ". FData($_POST['country_of_origin'], "Text"));
		$sqlB->Append(" , pattern_type = ". FData($_POST['pattern_type'], "Text"));
		$sqlB->Append(" , inner_material_type = ". FData($_POST['inner_material_type'], "Text"));
		
		$sqlB->Append(" , seasons = ". FData($_POST['seasons'], "Text"));
		$sqlB->Append(" , outer_material_type1 = " . FData($_POST['outer_material_type1'], "Text") . " ");
		$sqlB->Append(" , outer_material_type2 = " . FData($_POST['outer_material_type2'], "Text") . " ");
		$sqlB->Append(" , outer_material_type3 = " . FData($_POST['outer_material_type3'], "Text") . " ");
		$sqlB->Append(" , outer_material_type4 = " . FData($_POST['outer_material_type4'], "Text") . " ");
		$sqlB->Append(" , outer_material_type5 = " . FData($_POST['outer_material_type5'], "Text") . " ");


		
		$sqlB->Append(" , department_name1 = ". FData($_POST['department_name1'], "Text"));
		$sqlB->Append(" , department_name2 = ". FData($_POST['department_name2'], "Text"));
		$sqlB->Append(" , department_name3 = ". FData($_POST['department_name3'], "Text"));

		$sqlB->Append(" , style_keywords1 = ". FData($_POST['style_keywords1'], "Text"));
		$sqlB->Append(" , style_keywords2 = ". FData($_POST['style_keywords2'], "Text"));
		$sqlB->Append(" , style_keywords3 = ". FData($_POST['style_keywords3'], "Text"));
		$sqlB->Append(" , style_keywords4 = ". FData($_POST['style_keywords4'], "Text"));
		$sqlB->Append(" , style_keywords5 = ". FData($_POST['style_keywords5'], "Text"));

		$sqlB->Append(" , style_name = " . FData($_POST['style_name'], "Text"));
		$sqlB->Append(" , neck_style = " . FData($_POST['neck_style'], "Text"));
		$sqlB->Append(" , fit_type = " . FData($_POST['fit_type'], "Text"));
		$sqlB->Append(" , sleeve_type = " . FData($_POST['sleeve_type'], "Text"));
		$sqlB->Append(" , waist_style = " . FData($_POST['waist_style'], "Text"));
		$sqlB->Append(" , leg_style = " . FData($_POST['leg_style'], "Text"));
		$sqlB->Append(" , underwire_type = " . FData($_POST['underwire_type'], "Text"));

		$sqlB->Append(" , fulfillment_latency = Null ");

		$sqlB->Append(" , generic_keywords1 = " . FData($_POST['generic_keywords1'], "Text") . " ");
		$sqlB->Append(" , generic_keywords2 = " . FData($_POST['generic_keywords2'], "Text") . " ");
		$sqlB->Append(" , generic_keywords3 = " . FData($_POST['generic_keywords3'], "Text") . " ");
		$sqlB->Append(" , generic_keywords4 = " . FData($_POST['generic_keywords4'], "Text") . " ");
		$sqlB->Append(" , generic_keywords5 = " . FData($_POST['generic_keywords5'], "Text") . " ");
		//$sqlB->Append(" , fulfillment_latency = ". FData($_POST['fulfillment_latency'], "Text") . " ");
		$sqlB->Append(" WHERE ClothingImportSheet.ID = ".$ClothingImportSheetID . " ");
		//Debug($sqlB->ToString());
		$Model->execute($sqlB->ToString());
		
	

		//搜索ImportProductTransSheet表 ：子商品表，用来更新生成SKU以及价格库存等。
		$sqlB = new StringBuilder();
		$sqlB->Append("SELECT ");
		$sqlB->Append("   ClothingImportTransSheet.ID ");
		$sqlB->Append("FROM ClothingImportTransSheet ");
		$sqlB->Append("WHERE ClothingImportTransSheet.ParentID = ".$ClothingImportSheetID . " ");
		$sqlB->Append("ORDER BY ClothingImportTransSheet.ID;");
		$ClothingImportTransSheetList = $Model->query($sqlB->ToString());

		//更新ImportProductTransSheet表 SKU以及价格库存 ：子商品表。
		
		for ($i = 0; $i < count($ClothingImportTransSheetList); $i++)
		{		
			$sqlB = new StringBuilder();
			$sqlB->Append("UPDATE ClothingImportTransSheet ");
			$sqlB->Append("SET ");
			$sqlB->Append("   item_sku =" . FData($_POST['item_sku'][$ClothingImportTransSheetList[$i]['ID']], "Text"));
			$sqlB->Append(" , standard_price =" . FData($_POST['standard_price'][$ClothingImportTransSheetList[$i]['ID']], "Text") );
			$sqlB->Append(" , sale_price =" . FData($_POST['sale_price'][$ClothingImportTransSheetList[$i]['ID']], "Text") );
			$sqlB->Append(" , sale_from_date = " . FData($_POST['sale_from_date'][$ClothingImportTransSheetList[$i]['ID']], "Text") );
			$sqlB->Append(" , sale_end_date = " . FData($_POST['sale_end_date'][$ClothingImportTransSheetList[$i]['ID']], "Text" ));
			$sqlB->Append(" , quantity =" . FData($_POST['quantity'][$ClothingImportTransSheetList[$i]['ID']], "Text"). " " );
			$sqlB->Append("WHERE ClothingImportTransSheet.ID = ".$ClothingImportTransSheetList[$i]['ID'] . " ");
			$sqlB->Append(";");
			$Model->execute($sqlB->ToString());
		}

		//更新ImportProductTransSheet表 其他的一些字段 ：子商品表。
		$sqlB = new StringBuilder();
		$sqlB->Append("UPDATE ClothingImportTransSheet ");
		$sqlB->Append("SET ");
		if($product_description != ""){
			
			$sqlB->Append("   product_description = '" . $product_description  . "' ");
		}
		else{
			$sqlB->Append("   product_description = Null ");
		}
		
		if($safety_warning != ""){
			
			$sqlB->Append(" , safety_warning = '" . $safety_warning  . "' ");
		}
		else{
			$sqlB->Append("  , safety_warning = Null ");
		}
		
		$sqlB->Append(" , country_of_origin =  " . FData($_POST['country_of_origin'], "Text"));
		$sqlB->Append(" , inner_material_type = ". FData($_POST['inner_material_type'], "Text"));
		$sqlB->Append(" , pattern_type = ". FData($_POST['pattern_type'], "Text"));
		
		$sqlB->Append(" , seasons = " . FData($_POST['seasons'], "Text"));
		$sqlB->Append(" , outer_material_type1 = " . FData($_POST['outer_material_type1'], "Text") . " ");
		$sqlB->Append(" , outer_material_type2 = " . FData($_POST['outer_material_type2'], "Text") . " ");
		$sqlB->Append(" , outer_material_type3 = " . FData($_POST['outer_material_type3'], "Text") . " ");
		$sqlB->Append(" , outer_material_type4 = " . FData($_POST['outer_material_type4'], "Text") . " ");
		$sqlB->Append(" , outer_material_type5 = " . FData($_POST['outer_material_type5'], "Text") . " ");


		$sqlB->Append(" , department_name1 = ". FData($_POST['department_name1'], "Text"));
		$sqlB->Append(" , department_name2 = ". FData($_POST['department_name2'], "Text"));
		$sqlB->Append(" , department_name3 = ". FData($_POST['department_name3'], "Text"));

		$sqlB->Append(" , style_keywords1 = ". FData($_POST['style_keywords1'], "Text"));
		$sqlB->Append(" , style_keywords2 = ". FData($_POST['style_keywords2'], "Text"));
		$sqlB->Append(" , style_keywords3 = ". FData($_POST['style_keywords3'], "Text"));
		$sqlB->Append(" , style_keywords4 = ". FData($_POST['style_keywords4'], "Text"));
		$sqlB->Append(" , style_keywords5 = ". FData($_POST['style_keywords5'], "Text"));
		
		$sqlB->Append(" , generic_keywords1 = " . FData($_POST['generic_keywords1'], "Text") . " ");
		$sqlB->Append(" , generic_keywords2 = " . FData($_POST['generic_keywords2'], "Text") . " ");
		$sqlB->Append(" , generic_keywords3 = " . FData($_POST['generic_keywords3'], "Text") . " ");
		$sqlB->Append(" , generic_keywords4 = " . FData($_POST['generic_keywords4'], "Text") . " ");
		$sqlB->Append(" , generic_keywords5 = " . FData($_POST['generic_keywords5'], "Text") . " ");

		$sqlB->Append(" , style_name = " . FData($_POST['style_name'], "Text"));
		$sqlB->Append(" , neck_style = " . FData($_POST['neck_style'], "Text"));
		$sqlB->Append(" , fit_type = " . FData($_POST['fit_type'], "Text"));
		$sqlB->Append(" , sleeve_type = " . FData($_POST['sleeve_type'], "Text"));
		$sqlB->Append(" , waist_style = " . FData($_POST['waist_style'], "Text"));
		$sqlB->Append(" , leg_style = " . FData($_POST['leg_style'], "Text"));
		$sqlB->Append(" , underwire_type = " . FData($_POST['underwire_type'], "Text"));

		$sqlB->Append(" , parent_sku = ". FData($_POST['item_sku'][0], "Text") . " ");
		$sqlB->Append(" , fulfillment_latency = ". FData($_POST['fulfillment_latency'], "Text") . " ");
		$sqlB->Append("WHERE ClothingImportTransSheet.ParentID = " . $ClothingImportSheetID . " ");

		$Model->execute($sqlB->ToString());

		if($ReturnUrl != ""){
			$this->redirect("/ImportSheet/Edit");
		}
		else{
			$this->redirect("/ImportSheet/ViewParentsProduct");
		}
	}


	
	function CopyClothingImportSheet(){
		$ClothingImportSheetID = $_GET['ClothingImportSheetID'];
		
		if($ClothingImportSheetID != ""){

			$Model = new Model();
			$sqlB = new StringBuilder();
			$sqlB->Append("SELECT ");
			$sqlB->Append("   `TableFields`.`Field` ");
			$sqlB->Append("FROM `TableFields` ");
			$sqlB->Append("WHERE `TableFields`.`Table` = 'ClothingImportSheet' ");
			$sqlB->Append("ORDER BY Sequence ");
			$sqlB->Append(";");
			
			$TableFieldsList = $Model->query($sqlB->ToString());
			
			$sqlB = new StringBuilder();

			$sqlB->Append("INSERT INTO  ClothingImportSheet ");
			$sqlB->Append("(");

			for($i =0; $i< count($TableFieldsList); $i ++){
				if($TableFieldsList[$i]['Field'] != "ID"){
					$sqlB->Append( $TableFieldsList[$i]['Field'] . " ");
					if( count($TableFieldsList)-1 > $i){
						$sqlB->Append(" , ");
					}

					if($TableFieldsList[$i]['Field'] == "brand_name"){
					
						$sqlB->Append(" brand_name_en , ");
						$sqlB->Append(" brand_name_zh , ");
					}
				}
			}
			$sqlB->Append(")");
			$sqlB->Append("SELECT ");

			for($i =0; $i< count($TableFieldsList); $i ++){
				if($TableFieldsList[$i]['Field'] != "ID"){
					if($TableFieldsList[$i]['Field'] == "TimeUpdated" ||$TableFieldsList[$i]['Field'] == "TimeCreated"){
						$sqlB->Append(" NOW() AS " . $TableFieldsList[$i]['Field']);
					}
					elseif($TableFieldsList[$i]['Field'] == "item_name"){
						$sqlB->Append(" CONCAT(" . $TableFieldsList[$i]['Field'] . " , '_副本') AS " . $TableFieldsList[$i]['Field']. " ");
					}
					elseif($TableFieldsList[$i]['Field'] == "main_image_url" || $TableFieldsList[$i]['Field'] == "other_image_url1"|| $TableFieldsList[$i]['Field'] == "other_image_url2"|| $TableFieldsList[$i]['Field'] == "other_image_url3"|| $TableFieldsList[$i]['Field'] == "other_image_url4"|| $TableFieldsList[$i]['Field'] == "other_image_url5"|| $TableFieldsList[$i]['Field'] == "other_image_url6"|| $TableFieldsList[$i]['Field'] == "other_image_url7"|| $TableFieldsList[$i]['Field'] == "other_image_url8"|| $TableFieldsList[$i]['Field'] == "swatch_image_url"){
					
						$sqlB->Append("  Null AS " . $TableFieldsList[$i]['Field'] . " ");
					}
					else{
						$sqlB->Append( $TableFieldsList[$i]['Field'] . " ");
					}
					
					if( count($TableFieldsList)-1 > $i){
						$sqlB->Append(" , ");
					}

					if($TableFieldsList[$i]['Field'] == "brand_name"){
					
						$sqlB->Append(" brand_name_en , ");
						$sqlB->Append(" brand_name_zh , ");
					}
				}
			}
			$sqlB->Append("FROM ClothingImportSheet ");
			$sqlB->Append("WHERE ClothingImportSheet.ID = ".$ClothingImportSheetID . " ");
			$sqlB->Append("; ");

			$ClothingImportSheetIDStr = $Model->execute($sqlB->ToString());
			$ClothingImportSheetIDArr = explode("^", $ClothingImportSheetIDStr);
			
			
			$sqlB = new StringBuilder();
			$sqlB->Append("SELECT ");
			$sqlB->Append("   `TableFields`.`Field` ");
			$sqlB->Append("FROM `TableFields` ");
			$sqlB->Append("WHERE `TableFields`.`Table` = 'ClothingImportTransSheet' ");
			$sqlB->Append("ORDER BY Sequence ");
			$sqlB->Append(";");
			$TableFieldsList = $Model->query($sqlB->ToString());



			$sqlB = new StringBuilder();

			$sqlB->Append("INSERT INTO  ClothingImportTransSheet ");
			$sqlB->Append("(");

			for($i =0; $i< count($TableFieldsList); $i ++){
				if($TableFieldsList[$i]['Field'] != "ID"){
					$sqlB->Append( $TableFieldsList[$i]['Field'] . " ");
					if( count($TableFieldsList)-1 > $i){
						$sqlB->Append(" , ");
					}
				}
			}
			$sqlB->Append(")");
			$sqlB->Append("SELECT ");

			for($i =0; $i< count($TableFieldsList); $i ++){
				if($TableFieldsList[$i]['Field'] != "ID"){
					if($TableFieldsList[$i]['Field'] == "ParentID"){
						$sqlB->Append($ClothingImportSheetIDArr[1] . " AS " . $TableFieldsList[$i]['Field']);
					}
					elseif($TableFieldsList[$i]['Field']== "TimeUpdated" || $TableFieldsList[$i]['Field'] == "TimeCreated"){
						$sqlB->Append("  NOW() AS " . $TableFieldsList[$i]['Field'] . " ");
					}
					elseif($TableFieldsList[$i]['Field'] == "item_name"){
						$sqlB->Append(" CONCAT(" . $TableFieldsList[$i]['Field'] . " , '_副本') AS " . $TableFieldsList[$i]['Field']. " ");
					}
					elseif($TableFieldsList[$i]['Field'] == "main_image_url" || $TableFieldsList[$i]['Field'] == "other_image_url1"|| $TableFieldsList[$i]['Field'] == "other_image_url2"|| $TableFieldsList[$i]['Field'] == "other_image_url3"|| $TableFieldsList[$i]['Field'] == "other_image_url4"|| $TableFieldsList[$i]['Field'] == "other_image_url5"|| $TableFieldsList[$i]['Field'] == "other_image_url6"|| $TableFieldsList[$i]['Field'] == "other_image_url7"|| $TableFieldsList[$i]['Field'] == "other_image_url8"|| $TableFieldsList[$i]['Field'] == "swatch_image_url"){
					
						$sqlB->Append("  Null AS " . $TableFieldsList[$i]['Field'] . " ");
					}
					else{
						$sqlB->Append( $TableFieldsList[$i]['Field'] . " ");
					}

					if( count($TableFieldsList)-1 > $i){
						$sqlB->Append(" , ");
					}
				}
			}

			$sqlB->Append("FROM ClothingImportTransSheet ");
			$sqlB->Append("WHERE ClothingImportTransSheet.ParentID = ".$ClothingImportSheetID . " ");
			$sqlB->Append("; ");
			$Model->query($sqlB->ToString());
		}
		
		$this->redirect("/ImportSheet/ViewParentsProduct");
	}
}
